package org.tsinghua.answer.domain.user;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.tsinghua.answer.domain.entity.BaseEntity;
import org.tsinghua.answer.utils.Identities;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class User extends BaseEntity {

	@Enumerated(value=EnumType.STRING)
	private UserType type = UserType.USER;
	private String username;	
	private String password;
	private String nickname;
	private String email;
	private Sex sex;
	private String avatar;
	private String profile;
	private boolean verified;	//是否通过邮箱手机等验证
	private boolean authenticated;	//是否通过审核
	private boolean locked;	//账号是否被锁定
	@OneToMany(mappedBy="user", fetch=FetchType.EAGER)
	private List<UserSubject> userSubjects;
	//传值属性
	@Transient
	private List<String> subjectIds;
	
	@JsonIgnore
	@Transient
	private String oldPassword;	//旧密码，用于传值
	
	public User() {}
	
	public static User user(String username, String password
			, String nickname, String email, Sex sex, String profile) {
		User user = new User();
		user.setId(Identities.uuid2());
		user.setType(UserType.USER);
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname(nickname);
		user.setEmail(email);
		user.setSex(sex);
		user.setProfile(profile);
		user.setVerified(false);
		user.setAuthenticated(true);
		return user;
	}
	
	public static User volunteer(String username, String password
			, String nickname, String email, Sex sex, String profile) {
		User user = new User();
		user.setId(Identities.uuid2());
		user.setType(UserType.VOLUNTEER);
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname(nickname);
		user.setEmail(email);
		user.setSex(sex);
		user.setProfile(profile);
		user.setVerified(false);
		user.setAuthenticated(false);
		return user;
	}
	
	public static User administrator(String username, String password
			, String nickname, String email, Sex sex, String profile) {
		User user = new User();
		user.setId(Identities.uuid2());
		user.setType(UserType.ADMIN);
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname(nickname);
		user.setEmail(email);
		user.setSex(sex);
		user.setProfile(profile);
		user.setVerified(true);
		user.setAuthenticated(true);
		return user;
	}
	
	public UserType getType() {
		return type;
	}
	public void setType(UserType type) {
		this.type = type;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Sex getSex() {
		return sex;
	}
	public void setSex(Sex sex) {
		this.sex = sex;
	}
	public String getAvatar() {
		return avatar;
	}
	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}
	public String getProfile() {
		return profile;
	}
	public void setProfile(String profile) {
		this.profile = profile;
	}
	public boolean isVerified() {
		return verified;
	}
	public void setVerified(boolean verified) {
		this.verified = verified;
	}
	public boolean isAuthenticated() {
		return authenticated;
	}
	public void setAuthenticated(boolean authenticated) {
		this.authenticated = authenticated;
	}
	public boolean isLocked() {
		return locked;
	}
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public List<UserSubject> getUserSubjects() {
		return userSubjects;
	}
	public void setUserSubjects(List<UserSubject> userSubjects) {
		this.userSubjects = userSubjects;
	}
	public List<String> getSubjectIds() {
		return subjectIds;
	}
	public void setSubjectIds(List<String> subjectIds) {
		this.subjectIds = subjectIds;
	}
	
	public String getDisplayName() {
		if(StringUtils.isNotBlank(nickname)) {
			return nickname;
		} else {
			return username;
		}
	}

	public boolean isEnabled() {
		if(type == UserType.VOLUNTEER) {
			if(authenticated) {
				return true;
			} else {
				return false;
			}
		}
		return true;
	}
}
